#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@__author:gaoxiaojun
'''
from selenium import webdriver
from func.logFunc import get_logger
class BasePage(object):
    '''
    --BasePage封装所有页面都公用
    '''
    def __init__(self,selenium_driver,base_url,pagetitle):
        self.driver = selenium_driver
        self.base_url = base_url
        self.pagetitle = pagetitle
        self.timeout =30
        self.log = get_logger()
        self.bylist =['id','name','class','tag','link','plink','css','xpath']
        self.open()

        # '''命令分发'''
        # if hasattr(self, types):
        #     func = getattr(self, self.types)
        #     func(self.position, self.driver)

    # 通过title断言进入的页面是否正确。
    # 使用title获取当前窗口title，检查输入的title是否在当前title中，返回比较结果（True 或 False）
    def on_page(self, pagetitle):

        return pagetitle in self.driver.title

    # 打开页面，并校验页面链接是否加载正确
    def _open(self, url, pagetitle):
        self.driver.get(url)
        print('000')
        self.driver.maximize_window()
        # 使用assert进行校验，打开的窗口title是否与配置的title一致。调用on_page()方法
        print()
        assert self.on_page(pagetitle), u"打开开页面失败 %s" % url

    # 定义open方法，调用_open()进行打开链接
    def open(self):
        print("open")
        self._open(self.base_url, self.pagetitle)

    '''定义查找元素的方法'''
    def find_element_mothod(self,*loc):
        '''loc,传入的参数是有元组'''
        return  self.driver.find_element(*loc)

    def find_element(self, selector):
        '''传入的参数selector是list'''
        by = selector[0]
        value = selector[1]
        element = None
        if by in self.bylist:
            try:
                if by == 'id':
                    element = self.driver.find_element_by_id(value)
                elif by == 'name':
                    element = self.driver.find_element_by_name(value)
                elif by == 'class':
                    element = self.driver.find_element_by_class_name(value)
                elif by == 'tag':
                    element = self.driver.find_element_by_tag_name(value)
                elif by == 'link':
                    element = self.driver.find_element_by_link_text(value)
                elif by == 'plink':
                    element = self.driver.find_element_by_partial_link_text(value)
                elif by == 'css':
                    element = self.driver.find_element_by_css_selector(value)
                else :
                    element = self.driver.find_element_by_xpath(value)
                return element
            except Exception as e:
                self.log.error(e)
                print('e===?',e)
                raise AssertionError()
        else:
            self.log.error('输入的元素定位方式错误')
            raise AssertionError()

    def send(self, selector, value):
        element = self.find_element(selector)  # 调用封装的定位元素方法
        try:
            element.send_keys(value)
            self.log.info('输入的内容%s' % value)
        except BaseException as e :
            self.log('error %s ',e)

    def click(self, selector):
        element = self.find_element(selector)
        element.click()

    '''定义查找一组元素的方法'''
    def find_elements(self):
        by = selector[0]
        value = selector[1]
        elements=None
        if by in self.bylist:
            try:
                if by == 'id':
                    elements = self.driver.find_elements_by_id(id)
                elif by == 'name':
                    elements = self.driver.find_elements_by_name(name)
                elif by == 'classnaem':
                    elements = self.driver.find_elements_by_class_name(classnaem)
                elif by == 'tag':
                    elements = self.driver.find_elements_by_tag_name(tagname)
                elif by == 'link':
                    elements = self.driver.find_elements_by_link_text(text)
                elif by == 'plink':
                    elements = self.driver.find_elements_by_partial_link_text(text)
                elif by == 'xpath':
                    elements = self.driver.find_elements_by_xpath(xpath)
                else:
                    elements = self.driver.find_elements_by_css_selector(css)
                return elements
            except Exception as e:
                self.log.error('没有找到元素')
                raise AssertionError()
        else:
            self.log.error('输入的元素定位方式错误')
            raise AssertionError()


if __name__ == '__main__':
    from func.findEeleFun import getBrowserDriver
    url = "https://www.baidu.com/"
    driver =getBrowserDriver("Chrome")
    print("driver===",driver)
    BasePage(driver,url,'百度一下')